//
//  EFMyDistributorViewController.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/17.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFMyDistributorViewController.h"
#import "EFMentionDetailViewController.h"
#import "EFWthDrawalViewController.h"
#import "EFMyDistributorViewItem.h"
#import "EFMyDistributorViewCell.h"
#import "EFMyDistributorDetailItem.h"
#import "EFMentionDetailViewCell.h"

@interface EFMyDistributorViewController()<UITableViewDelegate, UITableViewDataSource>


@property(strong,nonatomic)UIView* topView;
@property(strong,nonatomic)UILabel* moneyLabel;
@property(strong,nonatomic)UILabel* moneyTipsLabel;

@property(strong,nonatomic)UIView* drawalView;
@property(strong,nonatomic)UILabel* drawalLabel;
@property(strong,nonatomic)UIButton* drawalBtn;
@property(strong,nonatomic)UIButton* drawalDetailBtn;

@property(strong,nonatomic)UILabel* tradeLabel;


@property(strong,nonatomic)NSMutableArray* dataArray;
@property(strong,nonatomic)UITableView* tableView;

@property(strong,nonatomic)NSNumber* available;

@end

@implementation EFMyDistributorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"我的分销";
        self.dataArray = [NSMutableArray array];
    }
    return self;
}

-(void)initView{
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.topView addSubview:self.moneyLabel];
    [self.topView addSubview:self.moneyTipsLabel];
    [self.view addSubview:self.topView];
    [self.drawalView addSubview:self.drawalLabel];
    [self.drawalView addSubview:self.drawalBtn];
    [self.drawalView addSubview:self.drawalDetailBtn];
    [self.view addSubview:self.drawalView];
    [self.view addSubview:self.tradeLabel];
    [self.view addSubview:self.tableView];
}

-(void)initData{
//    [self.moneyLabel setText:@"2500"];
//    [self.moneyLabel sizeToFit];
//    _moneyLabel.left = (kScreenWidth - self.moneyLabel.width)/2;
//    
//    [self.drawalLabel setText:@"当前可提现：250"];
//
//    
//    EFMyDistributorViewItem* item = [[EFMyDistributorViewItem alloc]init];
//    item.date = @"2017-01-01";
//    item.status = @"获得佣金";
//    item.money = @"25";
//    item.hide = true;
//    [self.dataArray addObject:item];
//    
//    EFMyDistributorViewItem* item2 = [[EFMyDistributorViewItem alloc]init];
//    item2.date = @"2017-01-01";
//    item2.status = @"花费佣金";
//    item2.money = @"-25";
//    item2.hide = true;
//    [self.dataArray addObject:item2];
    
    NSString* page = @"1";
    NSString* size = @"10";
    [self showHUD];
    __weak typeof(self) weakSelf = self;
    [APP_CONTEXT.net getDistributorWithPage:page size:size OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        [weakSelf.HUD hide:YES];
        if(code == 0){
            if ([info objectForKey:@"list"]) {
                NSArray *array = [info objectForKey:@"list"];
                for (NSDictionary *dic in array) {
                    EFMyDistributorViewItem *item = [EFMyDistributorViewItem yy_modelWithDictionary:dic];
                    [weakSelf.dataArray addObject:item];
                }
                
                if (array.count == 0) {
                    [weakSelf showHUDWithMessage:@"无更多内容"];
                }
            }
            NSNumber* totalCommission = [info objectForKey:@"totalCommission"]; //未提现佣金
            self.available = [info objectForKey:@"available"]; //可提现佣金
            
            [self.moneyLabel setText:totalCommission.description];
            [self.moneyLabel sizeToFit];
            _moneyLabel.left = (kScreenWidth - self.moneyLabel.width)/2;
            
            NSString* strAvaliable = [NSString stringWithFormat:@"当前可提现：%@",self.available];
            [self.drawalLabel setText:strAvaliable];
            
            [weakSelf.tableView reloadData];
        }else{
            
        }
    }];
    
    [self.tableView reloadData];
    
}

-(void)onClickDrawal{
    EFWthDrawalViewController* drawal = [[EFWthDrawalViewController alloc]initWithMoney:self.available.stringValue];
    [ICNavigator pushViewController:drawal animated:YES];
}

-(void)onClickDrawalDetail{
    EFMentionDetailViewController* mention = [[EFMentionDetailViewController alloc]init];
    [ICNavigator pushViewController:mention animated:YES];
    
}

-(UIView *)topView{
    if(_topView == NULL){
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 160)];
        _topView.backgroundColor = RGBCOLOR(0x7EAD6A);
    }
    return _topView;
}

-(UILabel *)moneyLabel{
    if(_moneyLabel == NULL){
        _moneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 47)];
        _moneyLabel.top = 46;
        _moneyLabel.font = [UIFont systemFontOfSize:53];
        _moneyLabel.textColor = [UIColor whiteColor];
    }
    return _moneyLabel;
}

-(UILabel *)moneyTipsLabel{
    if(_moneyTipsLabel == NULL){
        _moneyTipsLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 17)];
        _moneyTipsLabel.top = self.moneyLabel.bottom + 10;
        _moneyTipsLabel.font = [UIFont systemFontOfSize:15];
        _moneyTipsLabel.textColor = [UIColor whiteColor];
        [_moneyTipsLabel setText:@"未提现佣金"];
        [_moneyTipsLabel sizeToFit];
        _moneyTipsLabel.left = (kScreenWidth - self.moneyTipsLabel.width)/2;
    }
    return _moneyTipsLabel;
}

-(UIView *)drawalView{
    if(_drawalView == NULL){
        _drawalView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
        _drawalView.top = self.topView.bottom;
        _drawalView.backgroundColor = RGBCOLOR(0xF5F9F9);
    }
    return _drawalView;
}

-(UILabel *)drawalLabel{
    if(_drawalLabel == NULL){
        _drawalLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 17)];
        _drawalLabel.left = 20;
        _drawalLabel.top = 17;
        _drawalLabel.font = [UIFont systemFontOfSize:15];
        _drawalLabel.textColor = kBlack333;
    }
    return _drawalLabel;
}

-(UIButton *)drawalBtn{
    if(_drawalBtn == NULL){
        _drawalBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 54, 35)];
        _drawalBtn.top =    7;
        _drawalBtn.left = self.drawalDetailBtn.left - self.drawalBtn.width - 7;
        _drawalBtn.layer.masksToBounds = YES;
        _drawalBtn.layer.cornerRadius = 6;
        _drawalBtn.backgroundColor = RGBCOLOR(0x69BA45);
        _drawalBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        _drawalBtn.titleLabel.textColor = [UIColor whiteColor];
        [_drawalBtn setTitle:@"提现" forState:UIControlStateNormal];
        [_drawalBtn addTarget:self action:@selector(onClickDrawal) forControlEvents:UIControlEventTouchUpInside];
    }
    return _drawalBtn;
}

-(UIButton *)drawalDetailBtn{
    if(_drawalDetailBtn == NULL){
        _drawalDetailBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 37, 17)];
        _drawalDetailBtn.top =  16;
        _drawalDetailBtn.left = kScreenWidth - self.drawalDetailBtn.width - 20;
        _drawalDetailBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        _drawalDetailBtn.backgroundColor = [UIColor clearColor];
        [_drawalDetailBtn setTitleColor:RGBCOLOR(0x69BA45) forState:UIControlStateNormal];
        [_drawalDetailBtn setTitle:@"明细" forState:UIControlStateNormal];
        [_drawalDetailBtn addTarget:self action:@selector(onClickDrawalDetail) forControlEvents:UIControlEventTouchUpInside];
    }
    return _drawalDetailBtn;
}

-(UILabel *)tradeLabel{
    if(_tradeLabel == NULL){
        _tradeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 1020, 17)];
        _tradeLabel.left = 20;
        _tradeLabel.top = self.drawalView.bottom + 16;
        _tradeLabel.font = [UIFont systemFontOfSize:15];
        _tradeLabel.textColor = kBlack333;
        [_tradeLabel setText:@"交易明细"];
    }
    return _tradeLabel;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item = self.dataArray[indexPath.row];
    return item.viewHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem* item = self.dataArray[indexPath.row];
    NSString *identifier = NSStringFromClass(item.viewClass);
    ICTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell = [[item.viewClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if ([item isKindOfClass:[ICTableViewItem class]]) {
        if([item isKindOfClass:[EFMyDistributorDetailItem class]]){
            EFMentionDetailViewCell* detail = (EFMentionDetailViewCell*)cell;
            [detail setDistributorObject:(EFMyDistributorDetailItem*)item];
        }else{
            [cell setObject:item];
            
            ((EFMyDistributorViewCell *)cell).more = ^{
                [self itemClickMore:(EFMyDistributorViewItem *)item index:indexPath.row];
            };
            
        }
    }
    
    return cell;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusBarHeight - 260 )];
        _tableView.top = self.tradeLabel.bottom + 16;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return _tableView;
}

-(void)itemClickMore:(EFMyDistributorViewItem*) item index:(NSInteger)index{
    if(item.hide){
        NSInteger endIndex = 0 ;
        for(int i = index + 1;i< self.dataArray.count; ++i){
            int j = i+ 1;
            if(j < self.dataArray.count){
                if([[self.dataArray objectAtIndex:i] isKindOfClass:[EFMyDistributorDetailItem class]]){
                    endIndex++;
                    if(![[self.dataArray objectAtIndex:j] isKindOfClass:[EFMyDistributorDetailItem class]]){
                        break;
                    }
                }
            }else{
                if([[self.dataArray objectAtIndex:i] isKindOfClass:[EFMyDistributorDetailItem class]]){
                    endIndex++;
                }
            }
        }
        
        NSRange range = NSMakeRange(index + 1, endIndex);
        [self.dataArray removeObjectsInRange:range];
    }else{
        int start =   1;
        int realIndex = index + 1;
        for (NSDictionary *dic in item.detail) {
            EFMyDistributorDetailItem *item = [EFMyDistributorDetailItem yy_modelWithDictionary:dic];
            item.index = [NSString stringWithFormat:@"%d.", start++];
            [self.dataArray insertObject:item atIndex:realIndex++];
        }
    }
    item.hide = !item.hide;
    [self.tableView reloadData];
}




 

@end
